#pragma once
#include <iostream>

namespace YC
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _next;
		ListNode<T>* _prev;
		T _val;

		ListNode(const T& val = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _val(val)
		{}
	};

	template<class T, class Ref, class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;
		Node* _node;

		ListIterator(Node* node)
			:_node(node)
		{}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)
		{
			//Node* tmp = _node;
			//_node = _node->_next;
			Self tmp(*this);
			_node = _node->_next;

			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator--(int)
		{
			//Node* tmp = _node;
			//_node = _node->_prev;
			Self tmp(*this);
			_node = _node->_prev;

			return tmp;
		}

		Ref operator*()
		{
			return _node->_val;
		}

		Ptr operator->()
		{
			return &_node->_val;
		}

		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}

		bool operator==(const Self& it)
		{
			return _node == it._node;
		}
	};

	template<class T>
	class list
	{
	public:
		typedef ListNode<T> Node;
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;

		iterator begin()
		{
			return _head->_next;
		}

		iterator end()
		{
			return _head;
		}

		const_iterator begin() const
		{
			return _head->_next;
		}

		const_iterator end() const
		{
			return _head;
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		//void push_back(const T& x)
		//{
		//	Node* cur = new Node(x);
		//	Node* prev = _head->_prev;

		//	cur->_next = _head;
		//	_head->_prev = cur;

		//	prev->_next = cur;
		//	cur->_prev = prev;

		//	_size++;
		//}

		//void push_front(const T& x)
		//{
		//	Node* cur = new Node(x);
		//	Node* next = _head->_next;

		//	cur->_prev = _head;
		//	_head->_next = cur;

		//	next->_prev = cur;
		//	cur->_next = next;

		//	_size++;
		//}

		//void pop_front()
		//{
		//	if (_size == 1)
		//	{
		//		_head->_next = _head;
		//		_head->_prev = _head;
		//	}
		//	else
		//	{
		//		Node* next = _head->_next->_next;
		//		_head->_next = next;
		//		next->_prev = _head;
		//	}
		//	_size--;
		//}

		//void pop_back()
		//{
		//	if (_size == 1)
		//	{
		//		_head->_next = _head;
		//		_head->_prev = _head;
		//	}
		//	else
		//	{
		//		Node* prev = _head->_prev->_prev;
		//		_head->_prev = prev;
		//		prev->_next = head;
		//	}
		//	_size--;
		//}

		size_t size()
		{
			return _size;
		}

		bool empty()
		{
			return _size == 0;
		}

		void insert(iterator pos, const T& x)
		{
			Node* prev = pos._node->_prev;
			Node* cur = new Node(x);

			cur->_prev = prev;
			prev->_next = cur;

			cur->_next = pos._node;
			pos._node->_prev = cur;

			_size++;
		}

		iterator erase(iterator pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;
			_size--;

			return iterator(next);
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			_size = 0;
		}

		list()
		{
			empty_init();
		}

		list(const list<T>& lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

	private:
		Node* _head = nullptr;
		size_t _size = 0;
	};

	void test_list1()
	{
		list<int> lt;

		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		lt.push_front(10);
		lt.push_front(20);
		lt.push_front(30);

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

		lt.pop_back();
		lt.pop_back();
		lt.pop_front();
		lt.pop_front();

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}
};